package cc.leap.web;

import cc.leap.model.AjaxResult;
import cc.leap.model.TableDataInfo;
import cc.leap.model.domain.SysDictType;
import cc.leap.service.SysDictTypeService;
import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.extern.java.Log;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Post;
import org.noear.solon.annotation.Put;
import org.noear.solon.validation.annotation.Validated;

import java.util.List;

/**
 * SysDictTypeController
 *
 * @author leap
 * @date 2024/8/19
 */
@Controller
@Mapping("/system/dict/type")
public class SysDictTypeController {
    @Inject
    SysDictTypeService dictTypeService;

    @SaCheckPermission("system:dict:list")
    @Get
    @Mapping("/list")
    public TableDataInfo<SysDictType> list(SysDictType dictType) {
        return dictTypeService.selectDictTypePage(dictType);
    }

    /**
     * 查询字典类型详细
     */
    @SaCheckPermission("system:dict:query")
    @Get
    @Mapping(value = "/{dictId}")
    public AjaxResult getInfo(Long dictId) {
        return AjaxResult.success(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 获取字典选择框列表
     */
    @Get
    @Mapping("/optionselect")
    public AjaxResult optionselect() {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return AjaxResult.success(dictTypes);
    }

    /**
     * 新增字典类型
     */
    @SaCheckPermission("system:dict:add")
    @Post
    @Mapping
    public AjaxResult add(@Validated SysDictType dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return AjaxResult.error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return AjaxResult.success(dictTypeService.insertDictType(dict));
    }

    /**
     * 修改字典类型
     */
    @SaCheckPermission("system:dict:edit")
    @Put
    @Mapping
    public AjaxResult edit(@Validated SysDictType dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return AjaxResult.error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return AjaxResult.success(dictTypeService.updateDictType(dict));
    }

}
